#!/usr/bin/env python

"""
    Npc.py
    This file contains NPC information
"""

# Imports
from Creature import *
import random

class Npc(Object):
    def __init__(self, gameUI, gameMap, gameObjects, posy, posx, char, ticker, color, bolded = False, speed = 1, health = 1):
        self.posy = posy
        self.posx = posx
        self.char = char
        self.ticker = ticker
        self.color = color
        self.bolded = bolded
        self.speed = speed
        self.health = health

        self.gameUI = gameUI
        self.gameMap = gameMap
        self.gameObjects = gameObjects
        self.gamePC = gameObjects[0]

        self.ticker.scheduleTurn(self.speed, self)
        self.los = Bresenham(self.gameUI, self.gameMap, self.gameObjects)

        self.draw()

    def doTurn(self):
        if self.health > 0:
            processed = False
            self.clear()

            self.los.check(self.gamePC.posx, self.posx, self.gamePC.posy, self.posy)
            if self.los.blocked == False:
                self.dx = self.gamePC.posx - self.posx
                if abs(self.dx) > 0:
                    self.dx = self.dx / abs(self.dx)
                self.dy = self.gamePC.posy - self.posy
                if abs(self.dy) > 0:
                    self.dy = self.dy / abs(self.dy)
                if self.posy+self.dy < self.gameMap.height and self.posy+self.dy >= 0:
                    if self.posx+self.dx < self.gameMap.width and self.posx+self.dx >= 0:
                        if self.gameMap.map[self.posy+self.dy][self.posx+self.dx].walkable == True:
                            for obj in self.gameObjects:
                                if self.posy+self.dy == obj.posy and self.posx+self.dx == obj.posx and self.__class__.__name__ != obj.__class__.__name__:
                                    if obj.__class__.__name__ == self.gamePC.__class__.__name__:
                                        obj.health -= 1
                                        if obj.health == 0:
                                            obj.clear()
                                            self.gameObjects.remove(obj)
                                    processed = True
                            if processed == False:
                                self.clear()
                                self.move(self.dy, self.dx)
                                processed = True

            self.ticker.scheduleTurn(self.speed, self)
            if self.gameMap.map[self.posy][self.posx].fov == True:
                self.draw()
